package MawsonClient.GUI;

import MawsonClient.Config;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * User: Ryan Green
 * Date: Dec 8, 2010
 * Time: 1:11:30 PM
 * Copyright - University of South Australia
 */
public class MawsonTextField extends JTextField {
	boolean required;
	String defaultValue;
	private Color defaultColour;

	public MawsonTextField(String text, boolean required, JTextFieldLimit limit){
		super(text);

		this.setDocument(limit);

		defaultValue = text;
		this.setText(text);
		this.required = required;
		defaultColour = this.getBackground();

		this.addKeyListener(new KeyListener(){
			public void keyTyped(KeyEvent keyEvent) {}

			public void keyPressed(KeyEvent keyEvent) {}

			public void keyReleased(KeyEvent keyEvent) {
				changeColour();
			}
		});

		changeColour();
	}

	void changeColour(){
		if (required && this.getText().equals("")) {
			this.setBackground(Config.yellow);
		} else if (defaultValue.equals(this.getText()) && !defaultValue.equals("")){
			this.setBackground(Config.blue);
		}	else {
			this.setBackground(defaultColour);
		}
	}
}
